package com.lzx.shop.api;

import com.lzx.shop.dataobject.StoreDO;
import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.model.Result;
import com.lzx.shop.params.StoreParam;
import com.lzx.shop.service.StoreService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName StoreApi.java
 * @Description TODO
 * @createTime 2021年03月23日 00:05:00
 */
@Api(tags = "StoreApi",description = "店铺管理")
@RestController
@RequestMapping("/store")
public class StoreApi {

    @Autowired
    private StoreService storeService;

    @ApiOperation("查询所有店铺")
    @GetMapping("/list")
    public Result list(){
        return ResultUtil.customResultParam(true,"0","success",storeService.findAll());
    }

    @ApiOperation("插入店铺")
    @PostMapping("/save")
    public Result save(@RequestBody StoreDO object, HttpSession session){
        if (object==null){
            return ResultUtil.paramError();
        }
        UserDO userInfo = (UserDO) session.getAttribute("userInfo");
        object.setUserId(userInfo.getId());
        return ResultUtil.success(storeService.save(object));
    }

    @ApiOperation("更新店铺")
    @PostMapping("/update")
    public Result update(@RequestBody StoreDO object){
        if (object == null){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(storeService.update(object));
    }

    @ApiOperation("删除店铺")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") String id){
        if (StringUtils.isBlank(id)){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(storeService.DeleteById(id));
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/search")
    public Result search(@RequestBody StoreParam param){
        if (param==null){
            return ResultUtil.paramError();
        }
        return  ResultUtil.success(storeService.search(param));
    }
}
